import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'

import _ from 'lodash-es'



export const useSettingStore = () => {
    const router = useRouter()
    const languageJson = inject('languageJson')
    const languageType = inject('languageType')
    const userStore = useUserStore()
    const state = reactive({
        menulist: [
            {
                value: 'general',
                label: languageJson.value.setting.currency
            }, {
                value: 'company',
                label: languageJson.value.setting.company
            }, {
                value: 'default',
                label: languageJson.value.setting.placeOrder
            }, {
                value: 'hint',
                label: languageJson.value.setting.Soundandprompts
            }, {
                value: 'account',
                label: languageJson.value.setting.account
            }
        ],
        activemenu: 'general',

        formData: userStore.settingData,

        timezone: [],
        allcompany: false,
        islanguage: false,
        languageList: [
            {
                value: 'zh',
                label: '简体中文'
            }, {
                value: 'en',
                label: 'English'
            }
        ],
        oddstypeList: [
            {
                value: 'HongKong',
                label: languageJson.value.setting.HongKong
            },
            {
                value: 'Europe',
                label: languageJson.value.setting.Europe
            }
        ]

    })

    const gettimezone = () => {
        http.get('/config/get_timezone').then((res) => {
            state.timezone = res.data
        })
    }

    const changeformData = () => {
        console.log(state.formData);

        http.post("/config/set_user_config", state.formData).then((res) => {
            if (state.islanguage) {
                userStore.language.type = state.formData.general.language
                userStore.changelanguage()
            }
        })

    }

    const changeimezonecountry = () => {
        let result = state.timezone.find(item => item.country === state.formData.general.timezone.country);
        state.formData.general.timezone = result
        changeformData()
    }

    const changelanguage = () => {
        state.islanguage = true
        changeformData()
    }

    const changeallcompany = () => {
        state.formData.bc_types = []
        if (state.allcompany) {
            userStore.user.products.data_feed.list.forEach(item => {
                state.formData.bc_types.push(item.attr_code)
            });
        }
        changeformData()

    }

    onMounted(async () => {
        initData()
    })

    onUnmounted(() => {
    })


    async function initData() {
    }

    return {
        state,

        gettimezone,
        changelanguage,
        changeformData,
        changeimezonecountry,
        changeallcompany

    }
}
